<?php

namespace Beerus\Modules\DecoCompany\Actions;

use Doraemon\model\DecoCompany as mDecoCompany;
use Doraemon\service\DecoCompany\DecoCompany as sDecoCompany;
use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;

class SetBudgetTag extends RowAction
{
	public $name = '设置预算标签';

	public function handle($id, Request $request)
	{
		$budget_tag = array_filter($request->post('budget_tag'));
		$res = mDecoCompany::update([
			'budget_tag' => implode(',', $budget_tag),
			'edittime' => time()
		], [
			'id' => $id
		]);

		if ($res) {
			return $this->response()->success('操作成功(＾－＾)V')->refresh();
		}else{
			return $this->response()->success('操作失败,请稍后重试o(TωT)o');
		}
	}

	public function retrieveModel(Request $request)
	{
		if (!$key = $request->get('_key')) {
			return false;
		}
		return $key;
	}

	public function form($request)
	{
        $uid = $request->uid;
        $basic = sDecoCompany::getInfoByUid($uid);
        $budget_tag_options = mDecoCompany::BUDGET_TAG_DESC;
        $checked = explode(',', $basic['budget_tag']);
        $budget_tag_checked = [];
        foreach ($checked as $value) {
            if (array_key_exists($value, $budget_tag_options)) {
                $budget_tag_checked[$value] = $budget_tag_options[$value];
            }
        }
        $this->checkbox('budget_tag', '预算标签')->options($budget_tag_options)->checked($budget_tag_checked)->rules('required', ['required' => '预算标签不能为空']);
	}
}
